<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工单编码" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入工单编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工单名称" prop="orderName">
        <el-input
          v-model="queryParams.orderName"
          placeholder="请输入工单名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ruoyi-workorder:order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ruoyi-workorder:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ruoyi-workorder:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ruoyi-workorder:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工单ID" align="center" prop="id" />
      <el-table-column label="工单编码" align="center" prop="orderCode" />
      <el-table-column label="工单名称" align="center" prop="orderName" />
      <el-table-column label="工单内容" align="center" prop="orderContent" />
      <el-table-column label="是否已派单" align="center" prop="isAssigned">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isAssigned"/>
        </template>
      </el-table-column>
      <el-table-column label="派单人" align="center" prop="assignee" />
      <el-table-column label="是否已回复" align="center" prop="isReplied">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isReplied"/>
        </template>
      </el-table-column>
      <el-table-column label="回复内容" align="center" prop="replyContent" />
      <el-table-column label="回复图片" align="center" prop="replyImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.replyImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ruoyi-workorder:order:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ruoyi-workorder:order:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-send"
            @click="paisongxialakuang(scope.row)"
            v-hasPermi="['ruoyi-workorder:order:send']"
          >派送</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-huifu"
            @click="paisongxialakuang2(scope.row)"
            v-hasPermi="['ruoyi-workorder:order:huifu']"
          >回复</el-button>
<!--          <el-button type="text"  @click="paisongxialakuang(scope.row)">派单</el-button>-->
<!--          <el-button type="text"  @click="paisongxialakuang2(scope.row)">回复</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工单管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="工单名称" prop="orderName">
          <el-input v-model="form.orderName" placeholder="请输入工单名称" />
        </el-form-item>
        <el-form-item label="工单内容" prop="orderContent">
          <el-input v-model="form.orderContent" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="回复图片" prop="replyImage">
          <image-upload v-model="form.replyImage"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary"
                   @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--  派单功能  -->
    <el-dialog title="派单人员" :visible.sync="dialogFormVisible">
      <el-form :model="forms">
        <el-form-item label="派单人员" :label-width="formLabelWidth">
          <el-select v-model="forms.region" placeholder="请选择派单人员">
            <el-option label="admin" value="admin"></el-option>
            <el-option label="ry" value="ry"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
                  type="primary"
                   @click="paisong"
                  icon="el-icon-send"
                   v-hasPermi="['ruoyi-workorder:order:send']"
        >确定</el-button>
      </div>
    </el-dialog>
    <!--  回复功能  -->
    <el-dialog title="回复内容" :visible.sync="dialogFormVisible2">
      <el-form :model="forms">
        <el-form-item label="" :label-width="formLabelWidth">
          <el-form-item label="回复内容" :label-width="formLabelWidth">
            <el-input v-model="formss.name" autocomplete="off"></el-input>
          </el-form-item>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible2 = false">取 消</el-button>
        <el-button
                    type="primary"
                    icon="el-icon-huifu"
                   @click="paisong2"
                   v-hasPermi="['ruoyi-workorder:order:huifu']"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/ruoyi-workorder/order";
import axios from "axios";

export default {
  name: "Order",
  dicts: ['sys_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工单管理表格数据
      orderList: [],
      dialogTableVisible: false,
      dialogFormVisible: false,
      forms: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
      // 工单管理表格数据
      orderList2: [],
      dialogTableVisible2: false,
      dialogFormVisible2: false,
      formss: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
      formLabelWidth: '120px',
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderCode: null,
        orderName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderName: [
          { required: true, message: "工单名称不能为空", trigger: "blur" }
        ],
        orderContent: [
          { required: true, message: "工单内容不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    paisongxialakuang(row){
      this.dialogFormVisible= true
      this.forms.date1=row.id
      axios.get('/dev-api/system/user/getAllUserName')
        .then((resp)=>{
          this.forms.type=resp.data
        });
    },

    paisongxialakuang2(row){
      this.dialogFormVisible2= true
      this.formss.date1=row.id
    },

    //派单修改
    paisong(){
      let id=this.forms.date1
      let name=this.forms.region

      axios.get('/dev-api/ruoyi-workorder/order/send/' + id + '/' + name)
        .then((resp)=>{

        });
      this.getList()
      this.dialogFormVisible=false

    },

    //回复修改
    paisong2(){
      let id=this.formss.date1
      let name=this.formss.name

      axios.get('/dev-api/ruoyi-workorder/order/huifu/' + id + '/' + name)
        .then((resp)=>{

        });
      this.getList()
      this.dialogFormVisible2=false

    },
    /** 查询工单管理列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderCode: null,
        orderName: null,
        orderContent: null,
        isAssigned: null,
        assignee: null,
        isReplied: null,
        replyContent: null,
        replyImage: null,
        createdAt: null,
        updatedAt: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工单管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工单管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除工单管理编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ruoyi-workorder/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
